﻿using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BSF.Extensions;
using BSF.Db;
using BSF.BaseService.ShardingAdo;
using {t_namespace}.Model;

namespace {t_namespace}.Dal
{
	/*代码自动生成工具自动生成,不要在这里写自己的代码，否则会被自动覆盖哦 - 车江毅*/
	public partial class {t_object}AutoDal
    {
        public virtual bool Add(string shardingDb, {t_object}Model model)
        {
			SimpleProcedureParameter ps = new SimpleProcedureParameter();
			//分库信息
			ps.Add("@shardingDb",    shardingDb);
			<!-- BEGIN FieldList1 -->
			//{f_note1}
			ps.Add("@{f_name1}",    model.{f_name_model1});<!-- END FieldList1 -->   
            int rev = ShardingAdoHelper.ExecuteSql(@"/*db=@shardingDb*/insert into {t_object}(<!-- BEGIN FieldList2 -->{f_name2},<!-- END FieldList2 -->)
										   values(<!-- BEGIN FieldList3 -->@{f_name3},<!-- END FieldList3 -->)", ps.ToParameters());
            return rev == 1;

        }

        public virtual bool Edit(string shardingDb, {t_object}Model model)
        {
			SimpleProcedureParameter ps = new SimpleProcedureParameter();
            <!-- BEGIN FieldList4 -->
			//{f_note4}
			ps.Add("@{f_name4}",    model.{f_name_model4});<!-- END FieldList4 -->
			ps.Add("@{t_mainkeyname}",  model.{t_idFiled});
			ps.Add("@shardingDb",  shardingDb);

            int rev = ShardingAdoHelper.ExecuteSql("/*db=@shardingDb*/update {t_object} set <!-- BEGIN FieldList5 -->{f_name5}=@{f_name5},<!-- END FieldList5 --> where {t_mainkeyname}=@{t_mainkeyname}", ps.ToParameters());
            return rev == 1;

        }

        public virtual bool Delete(string shardingDb, {t_mainkeytype} {t_mainkeyname})
        {
            SimpleProcedureParameter ps = new SimpleProcedureParameter();
            ps.Add("@{t_mainkeyname}",  {t_mainkeyname});
			ps.Add("@shardingDb",  shardingDb);

            string Sql = "/*db=@shardingDb*/delete from {t_object} where {t_mainkeyname}=@{t_mainkeyname}";
            int rev = ShardingAdoHelper.ExecuteSql(Sql, ps.ToParameters());
            if (rev == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public virtual {t_object}Model Get(string shardingDb, {t_mainkeytype} {t_mainkeyname})
        {
            SimpleProcedureParameter ps = new SimpleProcedureParameter();
            ps.Add("@{t_mainkeyname}", {t_mainkeyname});
            StringBuilder stringSql = new StringBuilder();
            stringSql.Append(@"/*db=@shardingDb*/select s.* from {t_object} s where s.{t_mainkeyname}=@{t_mainkeyname}");
            var datas = ShardingAdoHelper.SqlToEntity<{t_object}Model>(stringSql.ToString(), ps.ToParameters());
            if (datas.Count > 0)
                return datas[0];
            return null;
        }

			<!-- BEGIN FieldList6 -->
			<!-- END FieldList6 -->

    }
}